/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package org.eclipsecon.dui.swtmodel.impl;

import java.util.Collection;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;

import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.InternalEList;

import org.eclipsecon.dui.swtmodel.MComposite;
import org.eclipsecon.dui.swtmodel.MModel;
import org.eclipsecon.dui.swtmodel.SwtmodelPackage;
import org.eclipsecon.dui.swtmodel.Template;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>MModel</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link org.eclipsecon.dui.swtmodel.impl.MModelImpl#getRootComposite <em>Root Composite</em>}</li>
 *   <li>{@link org.eclipsecon.dui.swtmodel.impl.MModelImpl#getTemplates <em>Templates</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class MModelImpl extends EObjectImpl implements MModel {
	/**
	 * The cached value of the '{@link #getRootComposite() <em>Root Composite</em>}' containment reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getRootComposite()
	 * @generated
	 * @ordered
	 */
	protected MComposite rootComposite;

	/**
	 * The cached value of the '{@link #getTemplates() <em>Templates</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getTemplates()
	 * @generated
	 * @ordered
	 */
	protected EList<Template> templates;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected MModelImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return SwtmodelPackage.Literals.MMODEL;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public MComposite getRootComposite() {
		return rootComposite;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetRootComposite(MComposite newRootComposite, NotificationChain msgs) {
		MComposite oldRootComposite = rootComposite;
		rootComposite = newRootComposite;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, SwtmodelPackage.MMODEL__ROOT_COMPOSITE, oldRootComposite, newRootComposite);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setRootComposite(MComposite newRootComposite) {
		if (newRootComposite != rootComposite) {
			NotificationChain msgs = null;
			if (rootComposite != null)
				msgs = ((InternalEObject)rootComposite).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - SwtmodelPackage.MMODEL__ROOT_COMPOSITE, null, msgs);
			if (newRootComposite != null)
				msgs = ((InternalEObject)newRootComposite).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - SwtmodelPackage.MMODEL__ROOT_COMPOSITE, null, msgs);
			msgs = basicSetRootComposite(newRootComposite, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, SwtmodelPackage.MMODEL__ROOT_COMPOSITE, newRootComposite, newRootComposite));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Template> getTemplates() {
		if (templates == null) {
			templates = new EObjectContainmentEList<Template>(Template.class, this, SwtmodelPackage.MMODEL__TEMPLATES);
		}
		return templates;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case SwtmodelPackage.MMODEL__ROOT_COMPOSITE:
				return basicSetRootComposite(null, msgs);
			case SwtmodelPackage.MMODEL__TEMPLATES:
				return ((InternalEList<?>)getTemplates()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case SwtmodelPackage.MMODEL__ROOT_COMPOSITE:
				return getRootComposite();
			case SwtmodelPackage.MMODEL__TEMPLATES:
				return getTemplates();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case SwtmodelPackage.MMODEL__ROOT_COMPOSITE:
				setRootComposite((MComposite)newValue);
				return;
			case SwtmodelPackage.MMODEL__TEMPLATES:
				getTemplates().clear();
				getTemplates().addAll((Collection<? extends Template>)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case SwtmodelPackage.MMODEL__ROOT_COMPOSITE:
				setRootComposite((MComposite)null);
				return;
			case SwtmodelPackage.MMODEL__TEMPLATES:
				getTemplates().clear();
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case SwtmodelPackage.MMODEL__ROOT_COMPOSITE:
				return rootComposite != null;
			case SwtmodelPackage.MMODEL__TEMPLATES:
				return templates != null && !templates.isEmpty();
		}
		return super.eIsSet(featureID);
	}

} //MModelImpl
